﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using week2.classes;

namespace week2
{
    public partial class Form1 : Form
    {
        bool iscredit;
        bool islogin;
        Bank bank = new Bank();
        int balance;
        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.BackColor = Color.LightBlue;
            iscredit = false;
            bank.CreateAccount("pyh1", "123");
            bank.CreateCreditAccount("pyh2", "123");
            textBox3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            islogin = true;
            label4.Text = "余额：" ;
            listBox1.Items.Clear();
            textBox1.Text = "pyh1";
            textBox2.Text = "123";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.BackColor = Color.LightBlue;
            button2.BackColor = Color.White;
            iscredit = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button2.BackColor = Color.LightBlue;
            button1.BackColor = Color.White;
            iscredit = true;
        }

        private void button3_Click(object sender, EventArgs e)//登录按钮
        {
            if (islogin) {
            string name = textBox1.Text;
            string pwd = textBox2.Text;
            ATM myATM = new ATM(bank, name, pwd);
                if (myATM.Logsuccess())
                {
                    Account account = myATM.GetAccount();
                    if (iscredit && account.GetType() != typeof(CreditAccount))
                    {
                        listBox1.Items.Add("原因：该账户为普通账户，请尝试切换类型重新登陆！");
                        MessageBox.Show("登陆失败！");
                    }
                    else if (!iscredit && account.GetType() != typeof(Account))
                    {
                        listBox1.Items.Add("原因：该账户为信用账户，请尝试切换类型重新登陆！");
                        MessageBox.Show("登陆失败！");
                    }
                    else
                    {
                        islogin = false;
                        listBox1.Items.Add("登陆成功！");
                        button3.Text = "退出";
                        textBox1.Enabled = false;
                        textBox2.Enabled = false;
                        button1.Enabled = false;
                        button2.Enabled = false;
                        textBox3.Enabled = true;
                        button4.Enabled = true;
                        button5.Enabled = true;
                        account.BigMoneyFetched += BigmoneyWarning;
                        label4.Text = "余额：" + account.GetBalance().ToString();
                    }
                }
                else
                {
                    listBox1.Items.Add("失败原因：用户名或密码错误！");
                    MessageBox.Show("登陆失败！");
                }
            }
            else
            {
                button3.Text = "登录";
                textBox3.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = false;
                textBox1.Enabled = true;
                textBox2.Enabled = true;
                button1.Enabled = true;
                button2.Enabled = true;
                islogin = true;
                label4.Text = "余额：";
                listBox1.Items.Clear();
            }
        }

        private void BigmoneyWarning(BigMoney bm)
        {
            listBox1.Items.Add("大金额取款=》用户名：“" + textBox1.Text + "”，金额：" + textBox3.Text);
        }

        private int Getbalance()
        {
            string name = textBox1.Text;
            string pwd = textBox2.Text;
            ATM myATM = new ATM(bank, name, pwd);
            if (myATM.Logsuccess())
            {
                Account account = myATM.GetAccount();
                return account.GetBalance();
            }
            return 0;
        }

        private void button4_Click(object sender, EventArgs e)//存入
        {
            if (textBox3.Text == "")
            {
                MessageBox.Show("请输入操作数额！");
                return;
            }
            int num = int.Parse(textBox3.Text);
            try { ATM.SaveM(num); }
            catch (BadMoneyException)
            {
                MessageBox.Show("操作失败！原因：假钞");
            }
            label4.Text = "余额：" + Getbalance().ToString();
            listBox1.Items.Add("存入成功！金额：" + textBox3.Text);
        }
    

        private void button5_Click(object sender, EventArgs e)//取出
        {
            if(textBox3.Text == "")
            {
                MessageBox.Show("请输入操作数额！");
                return;
            }
            int num = int.Parse(textBox3.Text);
            if (ATM.WithdrowM(num))
            {
                listBox1.Items.Add("取款成功！金额："+ textBox3.Text);
                MessageBox.Show("成功取出!");
            }
            else
            {
                MessageBox.Show("操作失败！");
            }
            label4.Text = "余额：" + Getbalance().ToString();
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '')//这是允许输入退格键
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
                {
                    e.Handled = true;
                }
            }
        }
    }
}
